package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.adapter.DoctorListAdapter;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.DoctorOrderSchedule;
import com.xywy.bloodandroid.bean.QuestionList;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.JsonUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;

/**
 * *****************************************
 * 
 * @Title: QuestionListAct.java
 * @Package com.xywy.bloodandroid.act
 * @Description: TODO(提问的问题列表)
 * @author smz
 * @date 2014-12-29 上午9:58:04
 * @version V1.0
 ******************************************* 
 */
public class QuestionListAct extends BaseActivity {

	private RelativeLayout left_img;
	private TextView middle_title;

	private List<QuestionList> templist;
	private int psize = 20;// 每一页的条数
	private int pagenum = 1;// 页数
	private DoctorListAdapter adapter;
	private ListView listview;
	private RelativeLayout rel;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_question_list);

		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("问医生");
		templist = new ArrayList<QuestionList>();
		listview = (ListView) findViewById(R.id.listview);
		adapter = new DoctorListAdapter(context, templist);
		listview.setAdapter(adapter);

		rel = (RelativeLayout) findViewById(R.id.rel);

	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		rel.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		getQuestionList();
	}

	/**
	 * 获取问题的列表
	 */
	private void getQuestionList() {
		GetRequest<String> getRequest = new GetRequest<String>(
				Constants.getUrl("club", "Club_QuesList") + "&userid="
						+ User.getInstance(context).getUserid()
						+ "&fromurl=app_xuey" + "&psize=" + psize + "&pagenum="
						+ pagenum, String.class,
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context,
								context.getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						try {
							JSONObject object = new JSONObject(response);
							int state = object.optInt("state");
							String data = object.optString("data");
							if (state == 200) {
								templist = (List<QuestionList>) JsonUtil
										.parseJsonToList(
												data,
												new TypeToken<List<QuestionList>>() {
												}.getType());
								 if(templist==null)
								 templist=new ArrayList<QuestionList>();
								// System.out.println("获取的lsit的集合..."+templist.size());
								// adapter.notifyDataSetChanged();
									adapter = new DoctorListAdapter(context,
											templist);
									listview.setAdapter(adapter);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
		getRequest.setIsParseJson(false);
		executeRequest(getRequest);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.rel:
			Intent intent = new Intent(context, FreeAskQuestionAct.class);
			intent.putExtra("mark", 100);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}
